package com.moonshineproject.view.notifier
{
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.containers.Canvas;
	import mx.events.EffectEvent;
	
	public class ActionNotifier extends Canvas
	{
		private static var instance:ActionNotifier;
		
		public static function getInstance():ActionNotifier {
			
			if (!instance)
				instance = new ActionNotifier();
			
			return instance;	
		}
		
		protected var showTimer:Timer;
		
		protected var notifyQueue:Array = [];
		
		protected var isShowing:ActionNotifyItem;
		
		public function notify(about:String):void {
			notifyQueue.push(about);
			checkQueue();
		}
		
		protected function checkQueue(e:Object=null):void {
			if (isShowing) return;
			
			if (notifyQueue.length > 0)
				showNew();
		}
		
		protected function showNew():void {
			var item:ActionNotifyItem = new ActionNotifyItem();
			item.notifyText = notifyQueue.pop();
			addChild(item);
			
			isShowing = item;
			
			showTimer = new Timer(500, 1);
			showTimer.addEventListener(TimerEvent.TIMER_COMPLETE, showDone);
			showTimer.start();
		}
		
		protected function showDone(e:Object=null):void {
			if (isShowing.alpha == 1) {
				isShowing.removeEffect.addEventListener(EffectEvent.EFFECT_END, showDone);
				isShowing.removeEffect.play([isShowing]);
			} else {
				removeChild(isShowing);
				isShowing = null;
				checkQueue();	
			}
		}

	}
}